
#include "khan.hpp"
#include "vty.hpp"
#include "xsg_sp_cmd.hpp"
#include "xen_mmc_mng.hpp"
#include "xsg_sp_util.hpp"

#define XSG_SP_CFG_PATH "/SI/SP/config/com/"

#define MAKE_PATH(__filename__) (XSG_SP_CFG_PATH __filename__)

#define XSG_SP_ST_CFG 	MAKE_PATH("D4291.mdb")
#define XSG_SP_OSP_CFG 	MAKE_PATH("D4250.mdb")
#define XSG_SP_DSP_CFG 	MAKE_PATH("D4253.mdb")
#define XSG_SP_LKS_CFG 	MAKE_PATH("D4260.mdb")
#define XSG_SP_SRTE_CFG MAKE_PATH("D4280.mdb")
#define XSG_SP_SLK_CFG 	MAKE_PATH("D4270.mdb")

#define XSG_SP_ASP_CFG 	MAKE_PATH("D4350.mdb")
#define XSG_SP_AS_CFG 	MAKE_PATH("D4360.mdb")

#define XSG_SP_ASP_ADDR_CFG 	MAKE_PATH("D4355.mdb")
#define XSG_SP_BIND_AS_ASP_CFG	MAKE_PATH("D4364.mdb")

void xsg_sp_runcfg(vty_ptr vty);
void xsg_st_runcfg(vty_ptr vty, const std::string& line);
void xsg_osp_runcfg(vty_ptr vty, const std::string& line);
void xsg_dsp_runcfg(vty_ptr vty, const std::string& line);
void xsg_lsk_runcfg(vty_ptr vty, const std::string& line);
void xsg_srte_runcfg(vty_ptr vty, const std::string& line);
void xsg_slk_runcfg(vty_ptr vty, const std::string& line);

void xsg_asp_runcfg(vty_ptr vty, const std::string& line);
void xsg_as_runcfg(vty_ptr vty, const std::string& line);
void xsg_asp_addr_runcfg(vty_ptr vty, const std::string& line);
void xsg_bind_as_asp_runcfg(vty_ptr vty, const std::string& line);

template <typename Function>
int xsg_read_mdb(vty_ptr vty, const std::string& mdbname, Function function) 
{
	int num=0;
	std::string fname(mdbname);
	std::ifstream fs(fname.c_str());
	std::string line;
	while (getline(fs, line)) {
		function(vty, line);
		num++;
	}
	fs.close();
};

void xsg_sp_runcfg(vty_ptr vty) 
{
	int num=0;
	num = xsg_read_mdb(vty, XSG_SP_ST_CFG, xsg_st_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_OSP_CFG, xsg_osp_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_DSP_CFG, xsg_dsp_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_LKS_CFG, xsg_lsk_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_SRTE_CFG, xsg_srte_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_SLK_CFG, xsg_slk_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);

	num = xsg_read_mdb(vty, XSG_SP_ASP_CFG, xsg_asp_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_AS_CFG, xsg_as_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_ASP_ADDR_CFG, xsg_asp_addr_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
	num = xsg_read_mdb(vty, XSG_SP_BIND_AS_ASP_CFG, xsg_bind_as_asp_runcfg);
	if (num != 0) vty->output("!"VTY_NEWLINE);
}

/*
2 ,256 ,SG1 ,2 ,0 ,REQ_SPACE
2 ,256 ,SG1 ,2 ,0 ,REQ_SPACE
0 ,512 ,SG2 ,3 ,1 ,REQ_SPACE 	*/

void xsg_st_runcfg(vty_ptr vty, const std::string& line)
{
#define ST_FMT "  st %d type %s\r\n"

	int  stnum, sttype; 
	char tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%d ,%d ,%s",
			&stnum, &sttype, tmp);
	if (sttype == 0) {
		sprintf(rstline, ST_FMT, stnum, "mtp2");
	} else if (sttype == 1) {
		sprintf(rstline, ST_FMT, stnum, "m2pa");
	} else {
	}

	printf("%s", rstline);
	vty->output(rstline);
}


//void xsg_osp_runcfg(vty_ptr vty)
void xsg_osp_runcfg(vty_ptr vty, const std::string& line)
{
#define OSP_FMT "  osp 0x%X %s ni %s variant %s type %s\r\n"

	int opc, nitype, variant, sptype;
	char ospn[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%d ,%d ,%s ,%d ,%d ,%s",
			&nitype, &opc, ospn, &variant, &sptype, tmp);
	sprintf(rstline, OSP_FMT, opc, ospn, 
			change_ni_str(nitype).c_str(), 
			change_variant_str(variant).c_str(), 
			change_sptype(sptype).c_str());

	printf("%s", rstline);
	vty->output(rstline);
}


void xsg_dsp_runcfg(vty_ptr vty, const std::string& line) 
{
#define DSP_FMT "  dsp %s 0x%X %s"
	int  dpc, loop, type;
	char ospn[256], dspn[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%s ,%d ,%s ,%d ,%d ,%s",
			ospn, &dpc, dspn, &loop, &type, tmp);
	if (loop == 0 && type == 0) {
		sprintf(rstline, DSP_FMT "\r\n", ospn, dpc, dspn);
	} else if (loop == 1 && type == 0) {
		sprintf(rstline, DSP_FMT " loop\r\n", ospn, dpc, dspn);
	} else if (type == 1){
		if (loop == 0) sprintf(rstline, DSP_FMT " type ipsp\r\n", ospn, dpc, dspn);
		else sprintf(rstline, DSP_FMT " loop type ipsp\r\n", ospn, dpc, dspn);
	} else if (type == 2){
		if (loop == 0) sprintf(rstline, DSP_FMT " type alias\r\n", ospn, dpc, dspn);
		else sprintf(rstline, DSP_FMT " loop type alias\r\n", ospn, dpc, dspn);
	} else {
		sprintf(rstline, "unkown dsp format!!\r\n");
	}

	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_lsk_runcfg(vty_ptr vty, const std::string& line) 
{
#define LKS_FMT "  lks %s %s"
	char ospn[256], aspn[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%s ,%s ,%s", ospn, aspn, tmp);
	sprintf(rstline, LKS_FMT "\r\n", ospn, aspn);
	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_srte_runcfg(vty_ptr vty, const std::string& line) 
{
#define SRTE_FMT "  srte %s %s %s %d"
	int prio;
	char ospn[256], dspn[256], aspn[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%s ,%s ,%s ,%d ,%s", 
			ospn, aspn, dspn, &prio, tmp);
	sprintf(rstline, SRTE_FMT "\r\n", ospn, aspn, dspn, prio);
	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_slk_runcfg(vty_ptr vty, const std::string& line) 
{
#define SLK_FMT "  slk %s %s %d %d %d %d"
	int slc, st, trk, ts;
	char ospn[256], aspn[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%s ,%s ,%d ,%d ,%d ,%d ,%s", 
			ospn, aspn, &slc, &st, &trk, &ts, tmp);
	sprintf(rstline, SLK_FMT "\r\n", ospn, aspn, slc, st, trk, ts);
	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_asp_runcfg(vty_ptr vty, const std::string& line) 
{
#define ASP_FMT "  asp %d %s %d %d %s"
	int id, qos, uatype, port; 
	char uaname[256], addr[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%d ,%d ,%d ,%s ,%d ,%s",
			&id, &uatype, &port, addr, &qos, tmp);
	if (uatype == 1)  sprintf(uaname, "iua");
	if (uatype == 3)  sprintf(uaname, "m3ua");
	if (uatype == 4)  sprintf(uaname, "sua");
	else {
		sprintf(rstline, "unkown user type (%d)\r\n", uatype);
	}
	sprintf(rstline, ASP_FMT "\r\n", 
			id, addr, port, qos, uaname);
	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_as_runcfg(vty_ptr vty, const std::string& line) 
{
	/*
	sscanf(line.c_str(), "%d ,%s ,%s ,%d ,%d ,%d ,%d ,%d ,%s",
			&id, ospn, dspn );
	*/
}

void xsg_asp_addr_runcfg(vty_ptr vty, const std::string& line) 
{
#define ASP_ADDR_FMT "  asp %d add %s"
	int id ;
	char addr[256], tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%d ,%s ,%s", &id, addr, tmp);
	sprintf(rstline, ASP_ADDR_FMT "\r\n", id, addr);
	printf("%s", rstline);
	vty->output(rstline);
}

void xsg_bind_as_asp_runcfg(vty_ptr vty, const std::string& line)
{
#define ASP_BIND_FMT "  as %d bind %d %d"
	int id , rtctx, sgp;
	char tmp[256];
	char rstline[1024];

	sscanf(line.c_str(), "%d ,%d ,%d ,%s", &rtctx, &sgp, &id, tmp);
	sprintf(rstline, ASP_BIND_FMT "\r\n", rtctx, id, sgp);
	printf("%s", rstline);
	vty->output(rstline);
}


